﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utilities
{
    public class TreeNodeData
    {
        /// <summary>
        /// The type name of the entity wrapped in the node
        /// </summary>
        public string TypeName { get; set; }

        /// <summary>
        /// The identifier of the entity wrapped in the node
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// The text of the node
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// Whether the node is expanded
        /// </summary>
        public bool IsExpanded { get; set; }

        /// <summary>
        /// Whether the node is a leaf
        /// </summary>
        public bool IsLeaf { get; set; }

        /// <summary>
        /// The children nodes if the node is not a leaf
        /// </summary>
        public IList<TreeNodeData> Children { get; set; }

        public static TreeNodeData Parse(string nodeId)
        {
            string[] parts = nodeId.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);

            return new TreeNodeData
            {
                TypeName = parts[0].Trim(),
                Id = parts[1].Trim()
            };
        }

        public static TreeNodeData Create(string typeName, string id, string text, bool isExpanded, bool isLeaf)
        {
            return new TreeNodeData
            {
                TypeName = typeName,
                Id = id,
                Text = text,
                IsExpanded = isExpanded,
                IsLeaf = isLeaf
            };
        }
    }
}
